package com.cn.jinl.service;


import com.cn.jinl.api.IChenxingMachineService;
import com.cn.jinl.api.IDeviceService;
import com.cn.jinl.api.IOnDutyProductService;
import com.cn.jinl.dao.ChenxingMachineMapper;
import com.cn.jinl.dao.LineOtherMapper;
import com.cn.jinl.dao.LinePeiMapper;
import com.cn.jinl.dao.OnDutyProductMapper;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ChenxingMachineServiceImpl implements IChenxingMachineService {

    @Autowired
    private ChenxingMachineMapper chenxingMachineMapper;

    @Autowired
    private LineOtherMapper lineOtherMapper;

    @Autowired
    private OnDutyProductMapper onDutyProductMapper;


    @Autowired
    private IDeviceService deviceService;

    @Override
    public Map<String, Object> getChenxingMachineMaps() throws Exception {
        Map<String,String>dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        ChenxingMachineExample example = new ChenxingMachineExample();
        example.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ChenxingMachine>chenxingMachines = chenxingMachineMapper.selectByExample(example);
        Map<String,ChenxingMachine> chenxingMap = Maps.newHashMap();
        int totalNum = 0;
        int openNum = 0;
        for(ChenxingMachine machine :chenxingMachines){
            chenxingMap.put(machine.getDeviceCode(),machine);
        }

        Map<String,List<String>> deviceMaps = deviceService.getDeviceByProcName("成型","");
        List<ChenxingMachine> results = Lists.newArrayList();

        for(String key :deviceMaps.keySet() ){
            List<String> deviceCodes = deviceMaps.get(key);
            for(String code : deviceCodes){
                ++totalNum;
                if(chenxingMap.containsKey(code)){
                    results.add(chenxingMap.get(code));
                    if(StringUtils.equals("1",chenxingMap.get(code).getQjStatus())){
                        ++openNum;
                    }
                }else{
                    ChenxingMachine add = new ChenxingMachine();
                    add.setDeviceCode(code);
                    results.add(add);
                }
            }

        }
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("chenxingMachines",results);
        resultMap.put("openNum",openNum);
        resultMap.put("totalNum",totalNum);
        resultMap.put("waitingNum",totalNum - openNum);
        return resultMap;
    }

    @Override
    public Map<String, String> getChenxingMachineStatusMaps() throws Exception {
        Map<String,String>dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        ChenxingMachineExample example = new ChenxingMachineExample();
        example.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<ChenxingMachine>chenxingMachines = chenxingMachineMapper.selectByExample(example);
        int openNum = 0;
        Map<String,String>resultMap = Maps.newHashMap();
        for(ChenxingMachine machine :chenxingMachines){
            if(StringUtils.equals("1",machine.getQjStatus())){
                ++openNum;
            }
            resultMap.put(machine.getDeviceCode(),machine.getGradeValue());
            resultMap.put(machine.getDeviceCode()+"#",machine.getPlanName());
        }
        resultMap.put("openNum",openNum+"");
        return resultMap;
    }

    @Override
    public String updateChenxingMachine(ChenxingMachine chenxingMachine) throws Exception {
        String id =  chenxingMachine.getId();
        int count = 0;
        Map<String,String>dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        if(StringUtils.isEmpty(id)){
            id = CommonUtil.generateRandomNum("chenxingMachine-");
            chenxingMachine.setId(id);
            chenxingMachine.setWhiteDay(whiteDay);
            chenxingMachine.setDutyDay(dutyDay);
            count = chenxingMachineMapper.insertSelective(chenxingMachine);
        }else {
            ChenxingMachineExample example = new ChenxingMachineExample();
            example.createCriteria().andIdEqualTo(id);
            count = chenxingMachineMapper.updateByExampleSelective(chenxingMachine,example);
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public ChenxingMachine getChenxingMachineByDeviceCode(String deviceCode) throws Exception {
        if(StringUtils.isEmpty(deviceCode)){
            return null;
        }

        Map<String,String>dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        ChenxingMachineExample example = new ChenxingMachineExample();
        example.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andStatusEqualTo(Constants.NORMAL_STATUS).andDeviceCodeEqualTo(deviceCode);
        List<ChenxingMachine>chenxingMachines = chenxingMachineMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(chenxingMachines)){
            return null;
        }
        return chenxingMachines.get(0);
    }

    @Override
    public List<GongXuDyVo> getDyDeviceGongXu() throws Exception {
        Map<String, Map<String, List<String>>> deviceMap = deviceService.getAllDeviceGroupByProcessName();
        if(deviceMap.entrySet().size() == 0){
            return Lists.newArrayList();
        }
        List<GongXuDyVo> gongXuDyVos = Lists.newLinkedList();
        Map<String,String>dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        ChenxingMachineExample example = new ChenxingMachineExample();
        example.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andStatusEqualTo(Constants.NORMAL_STATUS).andQjStatusEqualTo("1");
        int chengxingCount = chenxingMachineMapper.countByExample(example);



        LineOtherExample lineOtherExample = new LineOtherExample();
        lineOtherExample.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andStatusEqualTo(Constants.NORMAL_STATUS).andLineStatusEqualTo("1");
        List<LineOther> lineOthers = lineOtherMapper.selectByExample(lineOtherExample);
        List<String> lineCodes = Lists.newArrayList();
        for(LineOther line:lineOthers){
            lineCodes.add(line.getDeviceCode()) ;
        }


        OnDutyProductExample productExample = new OnDutyProductExample();
        productExample.createCriteria().andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay).andStatusEqualTo(Constants.NORMAL_STATUS).andProductStatusEqualTo("2");
        List<OnDutyProduct> onDutyProducts = onDutyProductMapper.selectByExample(productExample);
        List<String> productCodes = Lists.newArrayList();
        for(OnDutyProduct product:onDutyProducts){
            productCodes.add(product.getDeviceCode()) ;
        }

        for(String key : deviceMap.keySet()){
            Map<String, List<String>> itemMaps = deviceMap.get(key);
            for(String key1:itemMaps.keySet()){
                GongXuDyVo gongXuDyVo = new GongXuDyVo();
                gongXuDyVo.setProcessName(key);
                gongXuDyVo.setItemName(key1);
                List<String> totalCodes = itemMaps.get(key1);
                List<String> copyList = totalCodes.stream().collect(Collectors.toList());
                gongXuDyVo.setTotal(totalCodes.size()+"");
                if(StringUtils.equals(key,"成型")){
                    gongXuDyVo.setRunning(chengxingCount+"");
                }else if(StringUtils.equals(key,"包装")){
                    copyList.retainAll(productCodes);
                    gongXuDyVo.setRunning(copyList.size()+"");
                }else {
                    copyList.retainAll(lineCodes);
                    gongXuDyVo.setRunning(copyList.size()+"");
                }
                gongXuDyVos.add(gongXuDyVo);
            }
        }
        Collections.sort(gongXuDyVos, new Comparator<GongXuDyVo>() {
            @Override
            public int compare(GongXuDyVo o1, GongXuDyVo o2) {
                if(o1.getProcessName().compareTo(o2.getProcessName()) < 0){
                    return -1;
                }
                return 1;
            }
        });
        return gongXuDyVos;
    }
}
